package com.wmapp.web;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
//import net.sf.json.processors.JsonBeanProcessor;
import net.sf.json.util.PropertyFilter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wmapp.domain.Administrator;
import com.wmapp.domain.BroadcastClient;
import com.wmapp.domain.ConfLayout;
import com.wmapp.domain.ConfMessage;
import com.wmapp.domain.ConfParticipant;
import com.wmapp.domain.ConfProfile;
import com.wmapp.domain.ConfReservation;
import com.wmapp.domain.Conference;
import com.wmapp.domain.ConferenceStatus;
import com.wmapp.domain.LoginInfo;
import com.wmapp.domain.MCU;
import com.wmapp.domain.Participant;
import com.wmapp.domain.ParticipantGroup;
import com.wmapp.language.Lang;
import com.wmapp.service.ManagerService;
import com.wmapp.utils.HttpUtils;
import com.wmapp.utils.SimpleDateUtils;
import com.wmapp.domain.SipAccount;
@Controller
@RequestMapping("/manage")
public class ManagerController {
	private static Log log=LogFactory.getLog(ManagerController.class);
	
	private ManagerService managerService;
	
	private Lang lang;
	
	@Autowired
	public void setLang(Lang lang) {
		this.lang = lang;
	}

	@Autowired
	public void setManagerService(ManagerService managerService) {
		this.managerService = managerService;
	}

	@RequestMapping(value="/main",method=RequestMethod.GET)
	public String enterMainPage(){
		return "main";
	}
	// --------------------------MCU------------------------
	@RequestMapping(value="/mcu/list/json",method=RequestMethod.GET)
	@ResponseBody
	public String queryMCUListAsJSON(){
		String ret="";
		
		List<MCU> list = managerService.queryMCUList();
		JSONObject jObj = HttpUtils.domainList2GridRecords(list, MCU.class, MCU.GRID_COLS, "id");
		ret=jObj.toString();
		log.debug(ret);
		return ret;
	}
	
	@RequestMapping(value="/mcu",method=RequestMethod.GET)
	public String enterMCU(@RequestParam(value="opt",required=true) String opt,@RequestParam(value="id",required=false) String id,Model model){
		if(opt.equals(HttpUtils.OPT_TYPE_MODIFY)){
			MCU mcu=managerService.getMCUById(id);
			model.addAttribute("mcu", mcu);
		}
		return "mcu";
	}
	
	@RequestMapping(value="/mcu",method=RequestMethod.POST)
	@ResponseBody
	public String addMCU(MCU mcu){
		try {
			managerService.addMCU(mcu);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/mcu",method=RequestMethod.PUT)
	@ResponseBody
	public String modifyMCU(MCU mcu){
		try {
			managerService.modifyMCU(mcu);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/mcu/check",method=RequestMethod.GET)
	@ResponseBody
	public String isNameExist(@RequestParam(value="name",required=true) String name){
		return String.valueOf(managerService.isNameExist(name));
	}
	
	@RequestMapping(value="/mcu/{id}",method=RequestMethod.DELETE)
	@ResponseBody
	public String removeMCU(@PathVariable String id){
		try {
			managerService.removeMCU(id);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	// -------------administrator------------
	@RequestMapping(value="/admin",method=RequestMethod.GET)
	public String enterAdmin(@RequestParam(value="opt",required=true) String opt,@RequestParam(value="name",required=false) String name,Model model) throws Exception{
		if(opt.equals(HttpUtils.OPT_TYPE_MODIFY)){
			String pkey=new String(name.getBytes("ISO-8859-1"), "UTF-8");
			Administrator admin=managerService.getAdminByName(pkey);
			model.addAttribute("admin", admin);
		}
		return "admin";
	}
	
	@RequestMapping(value="/admin",method=RequestMethod.POST)
	@ResponseBody
	public String addAdmin(Administrator admin){
		try {
			managerService.addAdmin(admin);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/admin/list/json",method=RequestMethod.GET)
	@ResponseBody
	public  String queryAdminListAsJSON(){
		String ret="";
		
		List<Administrator> adminList=managerService.queryAdminList();
		JSONObject jObj = HttpUtils.domainList2GridRecords(adminList, Administrator.class, Administrator.GRID_COLS, "name");
		ret=jObj.toString();
		
		return ret;
	}
	
	@RequestMapping(value="/admin/{names}",method=RequestMethod.DELETE)
	@ResponseBody
	public String removeAdmin(@PathVariable String names){
		try {
			managerService.removeAdmin(names);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/admin",method=RequestMethod.PUT)
	@ResponseBody
	public String modifyAdmin(Administrator admin){
		try {
			managerService.modifyAdmin(admin);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/admin/check",method=RequestMethod.GET)
	@ResponseBody
	public String isAdminNameExist(@RequestParam(value="name",required=true) String name){
		return String.valueOf(managerService.isAdminNameExist(name));
	}

    // -------------sip account------------
	@RequestMapping(value="/sip",method=RequestMethod.GET)
	public String enterSip(@RequestParam(value="opt",required=true) String opt,@RequestParam(value="name",required=false) String name,Model model) throws Exception{
		if(opt.equals(HttpUtils.OPT_TYPE_MODIFY)){
			String pkey=new String(name.getBytes("ISO-8859-1"), "UTF-8");
			SipAccount sipAccount=managerService.getSipByName(pkey);
			model.addAttribute("sip", sipAccount);
		}
		return "sipaccount";
	}

	@RequestMapping(value="/sip",method=RequestMethod.POST)
	@ResponseBody
	public String addSip(SipAccount sipAccount){
		try {
			managerService.addSip(sipAccount);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}

	@RequestMapping(value="/sip/list/json",method=RequestMethod.GET)
	@ResponseBody
	public  String querySipListAsJSON(){
		String ret="";

		List<SipAccount> sipList=managerService.querySipList();
		JSONObject jObj = HttpUtils.domainList2GridRecords(sipList, SipAccount.class, SipAccount.GRID_COLS, "account");
		ret=jObj.toString();

		return ret;
	}

	@RequestMapping(value="/sip/{names}",method=RequestMethod.DELETE)
	@ResponseBody
	public String removeSip(@PathVariable String names){
		try {
			managerService.removeSip(names);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}

	@RequestMapping(value="/sip",method=RequestMethod.PUT)
	@ResponseBody
	public String modifySip(SipAccount sipAccount){
		try {
			managerService.modifySip(sipAccount);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}

	@RequestMapping(value="/sip/check",method=RequestMethod.GET)
	@ResponseBody
	public String isSipNameExist(@RequestParam(value="name",required=true) String name){
		return String.valueOf(managerService.isSipNameExist(name));
	}
	
	//--------------------ParticipantGroup-----------------
	@RequestMapping(value="/group",method=RequestMethod.GET)
	public String enterParticipantGroup(@RequestParam(value="opt",required=true) String opt,@RequestParam(value="key",required=false) String key,Model model) throws Exception{
		if(opt.equals(HttpUtils.OPT_TYPE_MODIFY)){
			String pkey=new String(key.getBytes("ISO-8859-1"), "UTF-8");
			ParticipantGroup domain=managerService.getParticipantGroupByName(pkey);
			model.addAttribute("model", domain);
		}
		return "participantGroup";
	}
	
	@RequestMapping(value="/group",method=RequestMethod.POST)
	@ResponseBody
	public String addParticipantGroup(ParticipantGroup domain){
		try {
			managerService.addParticipantGroup(domain);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/group/list/json",method=RequestMethod.GET)
	@ResponseBody
	public  String queryParticipantGroupListAsJSON(){
		String ret="";
		
		List<ParticipantGroup> list=managerService.queryParticipantGroupList();
		JSONObject jObj = HttpUtils.domainList2GridRecords(list, ParticipantGroup.class, ParticipantGroup.GRID_COLS, "name");
		ret=jObj.toString();
		
		return ret;
	}
	
	@RequestMapping(value="/group/combox",method=RequestMethod.GET)
	@ResponseBody
	public String groupCombox(HttpSession session){
		List<ParticipantGroup> list=managerService.queryParticipantGroupList();
		
		LoginInfo loginInfo=(LoginInfo)session.getAttribute("loginInfo");
		List<ParticipantGroup> last=new ArrayList<ParticipantGroup>();
		
		if(loginInfo.getAccesslevel().equals(Administrator.LEVEL_CONF)
				&&loginInfo.getGrouplimit().equals("true")){
			String group=loginInfo.getGroup();
			List<ParticipantGroup> temp=new ArrayList<ParticipantGroup>();
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				ParticipantGroup g = (ParticipantGroup) iterator.next();
				if(g.getName().equals(group))
					temp.add(g);
			}
			last=temp;
		}else{
			last=list;
		}
		
		return JSONArray.fromObject(last).toString();
	}
	
	@RequestMapping(value="/group/{keys}",method=RequestMethod.DELETE)
	@ResponseBody
	public String removeParticipantGroup(@PathVariable String keys){
		try {
			managerService.removeParticipantGroup(keys);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/group",method=RequestMethod.PUT)
	@ResponseBody
	public String modifyParticipantGroup(ParticipantGroup domain){
		try {
			managerService.modifyParticipantGroup(domain);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/group/check",method=RequestMethod.GET)
	@ResponseBody
	public String isParticipantGroupNameExist(@RequestParam(value="key",required=true) String key){
		return String.valueOf(managerService.isParticipantGroupNameExist(key));
	}
	
	//--------------------Participant-----------------
	@RequestMapping(value="/participant",method=RequestMethod.GET)
	public String enterParticipant(@RequestParam(value="opt",required=true) String opt,@RequestParam(value="key",required=false) String key,Model model){
		if(opt.equals(HttpUtils.OPT_TYPE_MODIFY)){
			Participant domain=managerService.getParticipantByKey(key);
			model.addAttribute("model", domain);
		}
		return "participant";
	}
	
	@RequestMapping(value="/addressbook/win",method=RequestMethod.GET)
	public String enterAddressBook(){
		return "winAddressBook";
	}
	
	@RequestMapping(value="/participant",method=RequestMethod.POST)
	@ResponseBody
	public String addParticipant(Participant domain){
		try {
			managerService.addParticipant(domain);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/participant/list/json",method=RequestMethod.GET)
	@ResponseBody
	public  String queryParticipantListAsJSON(HttpSession session){
		String ret="";
		
		List<Participant> list=managerService.queryParticipantList();
		
		LoginInfo loginInfo=(LoginInfo)session.getAttribute("loginInfo");
		List<Participant> last=new ArrayList<Participant>();
		
		if(loginInfo.getAccesslevel().equals(Administrator.LEVEL_CONF)
				&&loginInfo.getGrouplimit().equals("true")){
			String group=loginInfo.getGroup();
			List<Participant> temp=new ArrayList<Participant>();
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				Participant ptc = (Participant) iterator.next();
				if(!StringUtils.hasText(ptc.getGroup())||ptc.getGroup().equals(group))
					temp.add(ptc);
			}
			last=temp;
		}else{
			last=list;
		}
		
		JSONObject jObj = HttpUtils.domainList2GridRecords(last, Participant.class, Participant.GRID_COLS, "number");
		ret=jObj.toString();
		return ret;
	}
	
	@RequestMapping(value="/participant/invite/list/json",method=RequestMethod.GET)
	@ResponseBody
	public  String queryPtcsForInvite(HttpSession session){
		String ret="";
		
		List<Participant> list=managerService.queryParticipantList();
		
		LoginInfo loginInfo=(LoginInfo)session.getAttribute("loginInfo");
		List<Participant> last=new ArrayList<Participant>();
		
		if(loginInfo.getAccesslevel().equals(Administrator.LEVEL_CONF)
				&&loginInfo.getGrouplimit().equals("true")){
			String group=loginInfo.getGroup();
			List<Participant> temp=new ArrayList<Participant>();
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				Participant ptc = (Participant) iterator.next();
				if(!StringUtils.hasText(ptc.getGroup())||ptc.getGroup().equals(group))
					temp.add(ptc);
			}
			last=temp;
		}else{
			last=list;
		}
		
		JSONObject jObj = HttpUtils.domainList2GridRecords(last, Participant.class, Participant.GRID_COLS_FOR_INVITE, "number");
		ret=jObj.toString();
		
		return ret;
	}
	
	@RequestMapping(value="/participant/{keys}",method=RequestMethod.DELETE)
	@ResponseBody
	public String removeParticipant(@PathVariable String keys){
		try {
			managerService.removeParticipant(keys);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/participant",method=RequestMethod.PUT)
	@ResponseBody
	public String modifyParticipant(Participant domain){
		try {
			managerService.modifyParticipant(domain);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/participant/check",method=RequestMethod.GET)
	@ResponseBody
	public String isParticipantExist(@RequestParam(value="key",required=true) String key){
		return String.valueOf(managerService.isParticipantExist(key));
	}
	
	//--------------------ConfProfile-----------------
	@RequestMapping(value="/profile",method=RequestMethod.GET)
	public String enterConfProfile(@RequestParam(value="opt",required=true) String opt,@RequestParam(value="key",required=false) String key,Model model) throws Exception{
		if(opt.equals(HttpUtils.OPT_TYPE_MODIFY)){
			String pkey=new String(key.getBytes("ISO-8859-1"), "UTF-8");
			ConfProfile domain=managerService.getConfProfileByKey(pkey);
			model.addAttribute("model", domain);
		}
		return "confProfile";
	}
	
	@RequestMapping(value="/profile",method=RequestMethod.POST)
	@ResponseBody
	public String addConfProfile(ConfProfile domain){
		try {
			domain.setModifyTime(SimpleDateUtils.getCurrentDate());
			managerService.addConfProfile(domain);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/profile/list/json",method=RequestMethod.GET)
	@ResponseBody
	public  String queryConfProfileListAsJSON(){
		String ret="";
		
		List<ConfProfile> list=managerService.queryConfProfileList();
		JSONObject jObj = HttpUtils.domainList2GridRecords(list, ConfProfile.class, ConfProfile.GRID_COLS, "profileName");
		ret=jObj.toString();
		
		return ret;
	}
	
	@RequestMapping(value="/profile/json",method=RequestMethod.GET)
	@ResponseBody
	public  String getConfProfileAsJSON(@RequestParam(value="name",required=true) String name){
		ConfProfile profile=managerService.getConfProfileByKey(name);
		
		return JSONObject.fromObject(profile).toString();
	}
	
	@RequestMapping(value="/profile/combox",method=RequestMethod.GET)
	@ResponseBody
	public String queryProfileNamesAsJSON(){
		List<ConfProfile> list=managerService.queryConfProfileList();
		
		JSONArray jArray=new JSONArray();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			ConfProfile profile = (ConfProfile) iterator.next();
			jArray.add(profile.getProfileName());
		}
		return jArray.toString();
	}
	
	@RequestMapping(value="/profile/{keys}",method=RequestMethod.DELETE)
	@ResponseBody
	public String removeConfProfile(@PathVariable String keys){
		try {
			managerService.removeConfProfile(keys);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/profile",method=RequestMethod.PUT)
	@ResponseBody
	public String modifyConfProfile(ConfProfile domain){
		try {
			domain.setModifyTime(SimpleDateUtils.getCurrentDate());
			managerService.modifyConfProfile(domain);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/profile/check",method=RequestMethod.GET)
	@ResponseBody
	public String isConfProfileExist(@RequestParam(value="key",required=true) String key){
		return String.valueOf(managerService.isConfProfileExist(key));
	}
	//--------------------Conference-----------------
	@RequestMapping(value="/conf",method=RequestMethod.GET)
	public String enterConference(@RequestParam(value="opt",required=true) String opt,@RequestParam(value="key",required=false) String key,Model model){
		if(opt.equals(HttpUtils.OPT_TYPE_MODIFY)){
			Conference domain=managerService.getConferenceByKey(key);
			model.addAttribute("model", domain);
		}
		return "conference";
	}
	
	@RequestMapping(value="/conf",method=RequestMethod.POST)
	@ResponseBody
	public String addConference(Conference domain){
		try {
			managerService.addConference(domain);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/list/json",method=RequestMethod.GET)
	@ResponseBody
	public  String queryConferenceListAsJSON(HttpSession session){
		String ret="";
		
		List<Conference> list=managerService.queryConferenceList();
		
		LoginInfo loginInfo=(LoginInfo)session.getAttribute("loginInfo");
		List<Conference> last=new ArrayList<Conference>();
		
		if(loginInfo.getAccesslevel().equals(Administrator.LEVEL_CONF)
				&&loginInfo.getGrouplimit().equals("true")){
			String group=loginInfo.getGroup();
			List<Conference> temp=new ArrayList<Conference>();
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				Conference conference = (Conference) iterator.next();
				if(!StringUtils.hasText(conference.getGroup())||conference.getGroup().equals(group))
					temp.add(conference);
			}
			
			last=temp;
		}else{
			last=list;
		}
		
		JSONObject jObj = HttpUtils.domainList2GridRecords(last, Conference.class, Conference.ONLINE_GRID_COLS, "number");
		ret=jObj.toString();
		
		return ret;
	}
	
	@RequestMapping(value="/conf/{keys}",method=RequestMethod.DELETE)
	@ResponseBody
	public String removeConference(@PathVariable String keys){
		try {
			managerService.removeConference(keys);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
        @RequestMapping(value="/conf/pause/{keys}",method=RequestMethod.PUT)
	@ResponseBody
	public String pauseConference(@PathVariable String keys){
		try {
			managerService.pauseConference(keys);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
        @RequestMapping(value="/conf/restart/{keys}",method=RequestMethod.PUT)
	@ResponseBody
	public String restartConference(@PathVariable String keys){
		try {
			managerService.restartConference(keys);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf",method=RequestMethod.PUT)
	@ResponseBody
	public String modifyConference(Conference domain){
		try {
			managerService.modifyConference(domain);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/check",method=RequestMethod.GET)
	@ResponseBody
	public String isConferenceExist(@RequestParam(value="key",required=true) String key){
		return String.valueOf(managerService.isConferenceExist(key));
	}
	
	@RequestMapping(value="/conf/status/json",method=RequestMethod.GET)
	@ResponseBody
	public String getConfStatusAsJSON(@RequestParam(value="number",required=true) String number){
		try {
			ConferenceStatus confStatus=managerService.getConfStatus(number);
			
			JsonConfig jsonConfig=new JsonConfig();
			jsonConfig.registerPropertyExclusions(ConferenceStatus.class, new String[]{"panes","participants"});
			JSONObject ret=JSONObject.fromObject(confStatus, jsonConfig);
			
			ret.put("participants", HttpUtils.domainList2GridRecords(confStatus.getParticipants(), ConfParticipant.class, ConfParticipant.GRID_FIELDS_FOR_PRESENT, "p_callId"));
			ret.put("panes", JSONArray.fromObject(confStatus.getPanes()));
			return ret.toString();
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/control/layout",method=RequestMethod.PUT)
	@ResponseBody
	public String saveCustomLayout(ConfLayout layout){
		try {
			managerService.saveCoustomLayout(layout);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/control/layout/auto",method=RequestMethod.PUT)
	@ResponseBody
	public String autoLayout(@RequestParam(value="number",required=true) String number){
		try {
			managerService.autoLayout(number);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/control/sendMessage",method=RequestMethod.PUT)
	@ResponseBody
	public String sendMessage(ConfMessage confMessage){
		try {
			managerService.sendMessage(confMessage);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/control/hangup",method=RequestMethod.PUT)
	@ResponseBody
	public String hangUpCalls(@RequestParam(value="number",required=true) String number,@RequestParam(value="calls",required=true) String calls){
		try {
			managerService.hangUpCalls(number, calls.split(","));
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}

    @RequestMapping(value="/conf/control/invite",method=RequestMethod.PUT)
	@ResponseBody
	public String inviteParticipants(@RequestParam(value="number",required=true) String number,@RequestParam(value="calls",required=true) String calls){
		try {
			managerService.inviteParticipants(number, calls.split(","));
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/control/mute",method=RequestMethod.PUT)
	@ResponseBody
	public String muteCalls(@RequestParam(value="number",required=true) String number,
			@RequestParam(value="calls",required=true) String calls,
			@RequestParam(value="mute",required=true) String mute){
		try {
			managerService.muteCalls(number, calls.split(","),Boolean.valueOf(mute));
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/control/lecture",method=RequestMethod.PUT)
	@ResponseBody
	public String specifyLecture(@RequestParam(value="number",required=true) String number,
			@RequestParam(value="callId",required=false) String callId,
			@RequestParam(value="cancel",required=true) String cancel){
		try {
			managerService.specifyLecture(number, callId, Boolean.valueOf(cancel));
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/control/endConf",method=RequestMethod.PUT)
	@ResponseBody
	public String endConference(@RequestParam(value="number",required=true) String number){
		try {
			managerService.removeConference(number);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/control/callDetail",method=RequestMethod.GET)
	public String showCallDetail(@RequestParam(value="number",required=true) String number
			,@RequestParam(value="callId",required=true) String callId,Model model){
		model.addAttribute("model", managerService.fetchCallDetail(number, callId));
		return "callDetail";
	}
	
	@RequestMapping(value="/conf/control/invite/enter",method=RequestMethod.GET)
	public String enterInvitPage(){
		return "inviteCall";
	}
	
	@RequestMapping(value="/conf/control/invite",method=RequestMethod.POST)
	@ResponseBody
	public String inviteCalls(@RequestParam(value="number",required=true) String number
			,@RequestParam(value="participants",required=true) String participants){
		try {
			List list = (List)JSONArray.toCollection(JSONArray.fromObject(participants), ConfParticipant.class);
			managerService.inviteCalls(number, list);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/control/broadcast",method=RequestMethod.GET)
	@ResponseBody
	public String queryBroadcastClients(@RequestParam(value="number",required=true) String number){
		try {
			List<BroadcastClient> list=managerService.queryBroadcastClients(number);
			JSONObject jObj=HttpUtils.domainList2GridRecords(list, BroadcastClient.class, BroadcastClient.GRID_COL_FIELDS, "dialogId");
			return jObj.toString();
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	//-------------------- Conference reservation------------------
	@RequestMapping(value="/conf/reserve/enter",method=RequestMethod.GET)
	public String enterConfReserveInfoPage(){
		return "confReserve";
	}
	
	@RequestMapping(value="/conf/reserve/list/json",method=RequestMethod.GET)
	@ResponseBody
	public String listReservation(){
		//String ret="[{\"id\":\"1302069844265\",\"text\":\"test 中文\",\"start_date\":\"2011-04-06 01:00\",\"end_date\":\"2011-05-06 01:00\",\"rec_type\":\"day_2___#15\",\"event_length\":3600,\"confinfo\":{\"group\":\"\",\"number\":\"777\",\"name\":\"test 中文\",\"password\":\"hello111\",\"chairpassword\":\"222\",\"bandwidth\":\"1024\",\"maxAudioPorts\":\"7\",\"reservedAudioPorts\":\"false\",\"maxVideoPorts\":\"8\",\"reservedVideoPorts\":\"false\",\"callInType\":\"all\",\"encrypt\":\"enable\",\"topic\":\"11111Desc\",\"muteOnJoin\":\"true\",\"audioFormatsArray\":[\"g722\",\"g728\",\"g729\",\"mulaw\",\"alaw\"],\"h263cif4Enabled\":\"true\",\"h264Enabled\":\"true\",\"h264Definition\":\"HD720P\",\"h239Enabled\":\"true\",\"h239Bandwidth\":\"128\",\"h239Codec\":\"H.264\",\"textPosition\":\"bottom_center\",\"textFont\":\"middle\",\"textColor\":\"#8e92e5\",\"textBgEnabled\":\"false\",\"textBgColor\":\"#7b4de5\",\"broadcastEnabled\":\"false\",\"broadcastPassword\":\"\",\"broadcastBandwidth\":\"384\",\"broadcastNumLimit\":\"\",\"participants\":[{\"p_number\":\"1001\",\"p_nameOverrideValue\":\"pvx888\",\"p_autoInvite\":\"true\",\"p_ip\":\"127.0.0.1\",\"p_videoEnabled\":\"true\",\"p_bandwidth\":\"1920\",\"p_email\":\"\",\"p_name\":\"pvx\"}]}}]";
		JSONArray jArray=null;
		List<ConfReservation> list= managerService.queryConfReservations();
		
		JsonConfig config=new JsonConfig();
		config.setJsonPropertyFilter(new PropertyFilter(){
			
			@Override
			public boolean apply(Object source, String name, Object value) {
				return ignore(name);
			}
			
			private boolean ignore(String property){
				boolean ret=true;
				for (int i = 0; i < ConfReservation.LIST_FIELDS.length; i++) {
					if(property!=null&&property.equals(ConfReservation.LIST_FIELDS[i])){
						ret=false;
						break;
					}
				}
				return ret;
			}
		});
		
		jArray = JSONArray.fromObject(list, config);
		return jArray.toString();
	}
	
	@RequestMapping(value="/conf/reserve",method=RequestMethod.POST)
	@ResponseBody
	public String addReservation(ConfReservation reservation){
		try {
			managerService.addConfReservation(reservation);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/reserve",method=RequestMethod.PUT)
	@ResponseBody
	public String updateReservation(ConfReservation reservation){
		try {
			managerService.updateConfReservation(reservation);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
	
	@RequestMapping(value="/conf/reserve/json",method=RequestMethod.GET)
	@ResponseBody
	public String getReservation(@RequestParam(value="id",required=true) String id){
		JSONObject ret=new JSONObject();
		try {
			ConfReservation confReservation = managerService.getConfReservation(id);
			
			JsonConfig config=new JsonConfig();
			config.registerPropertyExclusions(ConfReservation.class, new String[]{"participantsJSON","encryptEnabled","encryptForced"});
			config.setJsonPropertyFilter(new PropertyFilter() {
				@Override
				public boolean apply(Object source, String name, Object value) {
					if(source.getClass().isAssignableFrom(ConfParticipant.class)&&ignore(name)){
						return true;
					}else{
						return false;
					}
				}
				
				public boolean ignore(String name){
					boolean ret=true;
					
					for (int i = 0; i < ConfParticipant.FIELDS_FOR_CONF_RESERVATION.length; i++) {
						if(name!=null&&name.equals(ConfParticipant.FIELDS_FOR_CONF_RESERVATION[i])){
							ret=false;
							break;
						}
					}
					return ret;
				}
			});
			
			JSONObject data=JSONObject.fromObject(confReservation,config);
			
			ret.put("success", "true");
			ret.put("data", data);
			return ret.toString();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			
			ret.put("success", "false");
			ret.put("data", lang.getMsg("operation.fail"));
			return ret.toString();
		}
	}
	
	@RequestMapping(value="/conf/reserve",method=RequestMethod.DELETE)
	@ResponseBody
	public String removeReservation(@RequestParam(value="id",required=true) String id){
		try {
			managerService.removeConfReservation(id);
			return lang.getMsg("operation.success");
		} catch (Exception e) {
			log.error(e.getMessage());
			return lang.getMsg("operation.fail");
		}
	}
}
